<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;

use app\common\model\ArticleClass as ArticleClassModel;
use think\Db;

class ArticleClass extends AdminBase
{
    protected $article_class_model;

    protected function _initialize()
    {
        parent::_initialize();
        $this->article_class_model = new ArticleClassModel();
    }
    public function index()
    {
        $article_class_list = $this->article_class_model->select();

        return $this->fetch('index',['article_class_list'=>$article_class_list]);
    }

    public function add()
    {
        return $this->fetch('add');
    }

    public function save()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();
            $validate_result = $this->validate($data, 'ArticleClass');

            if($validate_result != true){
                $this->error($validate_result);
            }else{
                if($this->article_class_model->allowField(true)->save($data)){
                    $this->success('保存成功');
                }else{
                    $this->error('保存失败');
                }
            }
        }
    }

    public function edit($id)
    {
        $article_class_list = $this->article_class_model->where('id',$id)->find();
        return $this->fetch('edit',['article_class_list'=>$article_class_list]);
    }

    public function update($id)
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();
            $validate_result = $this->validate($data, 'ArticleClass');

            if($validate_result != true){
                $this->error($validate_result);
            }else{
                if($this->article_class_model->allowField(true)->save($data,$id)){
                    $this->success('保存成功');
                }else{
                    $this->error('保存失败');
                }
            }
        }
    }

    public function del()
    {
        $id = input('id');
        if($this->article_class_model->where('id',$id)->delete()){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
}